package com.ruoyi.project.loan.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.loan.domain.RepaymentRecord;
import com.ruoyi.project.loan.service.IRepaymentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 还款记录Controller
 *
 * @author ruoyi
 * @date 2025-11-11
 */
@RestController
@RequestMapping("/loan/record" )
public class RepaymentRecordController extends BaseController {
    @Autowired
    private IRepaymentRecordService repaymentRecordService;

    /**
     * 查询还款记录列表(不分页)
     */
    @PreAuthorize("@ss.hasPermi('loan:record:list')" )
    @GetMapping("/all/list" )
    public AjaxResult allList(RepaymentRecord repaymentRecord) {
        List<RepaymentRecord> list = repaymentRecordService.selectRepaymentRecordList(repaymentRecord);
        return success(list);
    }

    /**
     * 查询还款记录列表
     */
    @PreAuthorize("@ss.hasPermi('loan:record:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(RepaymentRecord repaymentRecord) {
        startPage();
        List<RepaymentRecord> list = repaymentRecordService.selectRepaymentRecordList(repaymentRecord);
        return getDataTable(list);
    }

    /**
     * 导出还款记录列表
     */
    @PreAuthorize("@ss.hasPermi('loan:record:export')" )
    @Log(title = "还款记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, RepaymentRecord repaymentRecord) {
        List<RepaymentRecord> list = repaymentRecordService.selectRepaymentRecordList(repaymentRecord);
        ExcelUtil<RepaymentRecord> util = new ExcelUtil<RepaymentRecord>(RepaymentRecord.class);
        util.exportExcel(response, list, "还款记录数据" );
    }

    /**
     * 获取还款记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('loan:record:query')" )
    @GetMapping(value = "/{recordId}" )
    public AjaxResult getInfo(@PathVariable("recordId" ) Long recordId) {
        return success(repaymentRecordService.selectRepaymentRecordByRecordId(recordId));
    }

    /**
     * 新增还款记录
     */
    @PreAuthorize("@ss.hasPermi('loan:record:add')" )
    @Log(title = "还款记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RepaymentRecord repaymentRecord) {
        return toAjax(repaymentRecordService.insertRepaymentRecord(repaymentRecord));
    }

    /**
     * 修改还款记录
     */
    @PreAuthorize("@ss.hasPermi('loan:record:edit')" )
    @Log(title = "还款记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RepaymentRecord repaymentRecord) {
        return toAjax(repaymentRecordService.updateRepaymentRecord(repaymentRecord));
    }

    /**
     * 删除还款记录
     */
    @PreAuthorize("@ss.hasPermi('loan:record:remove')" )
    @Log(title = "还款记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}" )
    public AjaxResult remove(@PathVariable Long[] recordIds) {
        return toAjax(repaymentRecordService.deleteRepaymentRecordByRecordIds(recordIds));
    }
}
